/**
 * 商品档案数据持久化处理类
 * MerchandiseDao.java
 * Copyright (C) 2013 Gank
 * Simple Currency Trade System
 * 
 */
package com.gank.ct.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import com.gank.ct.common.BaseDao;
import com.gank.ct.domain.Merchandise;
import com.gank.ct.util.ChkUtil;

/**
 * Copyright (C) 2013:Gank<br/>
 * Project:Curreny Trade<br/>
 * Module ID:<br/>
 * Comments:Simple Currency Trade System<br/>
 * JDK version used:JDK1.6<br/>
 * Package:com.gank.ct.dao<br/>
 * Author:Michael Wu<br/>
 * Create Date:2013-11-6<br/>
 * Modified By:<br/>
 * Modified Date:<br/>
 * Why & What is modified:<br/>
 * Version:0.1<br/>
 */
public class MerchandiseDao extends BaseDao<Merchandise, Integer> implements
		IMerchandiseDao {

	/*
	 * 商品档案查询
	 * 
	 * @see
	 * com.zds.slms.dao.IMerchandiseDao#findMerchandise(com.zds.slms.domain.
	 * Merchandise)
	 */
	public List<com.gank.ct.domain.Merchandise> findMerchandise(
			Merchandise merchandise) {
		// 对象查询条件
		DetachedCriteria criteria = DetachedCriteria
				.forClass(Merchandise.class);
		if (null != merchandise) {
			if (!ChkUtil.isNullOrEmpty(merchandise.getId())) {
				criteria.add(Restrictions.eq("id", merchandise.getId()));
			}
			if (!ChkUtil.isNullOrEmpty(merchandise.getCode())) {
				criteria.add(Restrictions.eq("code", merchandise.getCode()));
			}
			if (!ChkUtil.isNullOrEmpty(merchandise.getName())) {
				criteria.add(Restrictions.like("name", merchandise.getName(),
						MatchMode.ANYWHERE));

			}
		}
		return findByCriteria(criteria);
	}

	/*
	 * 商品档案保存
	 * 
	 * @see
	 * com.zds.slms.dao.IMerchandiseDao#saveMerchandise(com.zds.slms.domain.
	 * Merchandise)
	 */
	public void saveMerchandise(Merchandise merchandise) {
		save(merchandise);

	}

	/*
	 * 商品档案删除
	 * 
	 * @see
	 * com.zds.slms.dao.IMerchandiseDao#deleteMerchandise(java.lang.String[])
	 */
	public void deleteMerchandise(String[] merchandiseId) {
		List<Merchandise> entities = new ArrayList<Merchandise>();
		for (String mid : merchandiseId) {
			entities.add(load(Integer.valueOf(mid)));
		}
		// 批量删除
		deleteAll(entities);

	}

	/*
	 * 商品档案更新
	 * 
	 * @see
	 * com.zds.slms.dao.IMerchandiseDao#updateMerchandise(com.zds.slms.domain
	 * .Merchandise)
	 */
	public void updateMerchandise(Merchandise merchandise) {
		Integer mid = merchandise.getId();
		// 载入已经被持久化了的对象然后再进行修改
		Merchandise perstMerchandise = load(Integer.valueOf(mid));
		perstMerchandise.setCode(merchandise.getCode());
		perstMerchandise.setName(merchandise.getName());
		perstMerchandise.setPrice(merchandise.getPrice());

		update(perstMerchandise);

	}

}
